package com.example.mssagedigest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.example.nssagedigest.R;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {
    private String pw="Longyue113";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //button点击运算
    public void messageDigest(View view) {
        switch (view.getId()){
            case R.id.md5://32位
                messageDigest("MD5",pw);

                break;

            case R.id.sha1://40位

                break;
        }
    }


    //消息摘要的运算方法已经创建
    private String messageDigest(String type, String data) {
        try {
            //1、创建消息摘要的对象
            MessageDigest messageDigest=MessageDigest.getInstance(type);
            //2/调用对象的方法，进行运算
            messageDigest.update(data.getBytes());

            //hash
            byte[] digest = messageDigest.digest();

            //3.将字节数组转换成该样式:例如:8bc67131766fc5d9b7b40bb4831b5203
            //规律:0-9,a-f,十六进制表示数据

            //3.1打印一下运算后的数据
            Log.i("TAG", "----------->messageDiagest:hash字节长度" +digest.length);

            for (int i = 0; i < digest.length; i++) {
                Log.i("TAG", "----------->meassage:" +digest[i]);
            }




        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }
}
